package up_down_share.component;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.provider.Settings.Secure;





// ----------------------------------------------------------------------------------------------------
// ----- Application Delegate -----
// ----------------------------------------------------------------------------------------------------
public class AppDelegate extends Application
{
	// --------------------------------------------------
	// ----- constants -----
	// --------------------------------------------------

	// --------------------------------------------------
	// ----- properties -----
	// --------------------------------------------------

	// --------------------------------------------------
	// ----- extends -----
	// --------------------------------------------------
	@Override
	public void onCreate()
	{
		super.onCreate();

		loadSharedPreference();
	}

	@Override
	public void onTerminate()
	{
		synchronizeSharedPreference();
		super.onTerminate();
	}

	@Override
	public void onLowMemory()
	{
		super.onLowMemory();
	}

	// --------------------------------------------------
	// ----- functions -----
	// --------------------------------------------------
	public int getAppIconID()
	{
		return getApplicationInfo().icon;
	}

	public String getAppID()
	{
		try
		{
			final int GET_ALL = 0;
			final PackageInfo oPackageInfo = getPackageManager().getPackageInfo(getPackageName(), GET_ALL);
			final String szApplicationID = oPackageInfo.packageName;
			if (szApplicationID != null)
			{
				return szApplicationID;
			}
		}
		catch (final Exception ex)
		{
			up_down_share.library.Utility.log(AppDelegate.class, "Throws exception (getAppID).", ex);
		}
		return "";
	}

	public String getAppName()
	{
		return getString(getApplicationInfo().labelRes);
	}

	public String getAppVersion()
	{
		try
		{
			final int GET_ALL = 0;
			final PackageInfo oPackageInfo = getPackageManager().getPackageInfo(getPackageName(), GET_ALL);
			final String szApplicationVersion = oPackageInfo.versionName;
			if (szApplicationVersion != null)
			{
				return szApplicationVersion;
			}
		}
		catch (final Exception ex)
		{
			up_down_share.library.Utility.log(AppDelegate.class, "Throws exception (getAppVersion).", ex);
		}
		return "";
	}

	public Context getContext()
	{
		return this;
	}

	public String getDeviceID()
	{
		final String szDeviceID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
		if (szDeviceID != null)
		{
			return szDeviceID;
		}
		return "";
	}

	public void loadSharedPreference()
	{
		try
		{
			final SharedPreferences sharedPreferences = getSharedPreferences(getAppID(), MODE_PRIVATE);
		}
		catch (final Exception ex)
		{
			up_down_share.library.Utility.log(AppDelegate.class, "Throws exception (loadSharedPreference).", ex);
		}
	}

	public void synchronizeSharedPreference()
	{
		try
		{
			final SharedPreferences.Editor editor = getSharedPreferences(getAppID(), MODE_PRIVATE).edit();
			editor.commit();
		}
		catch (final Exception ex)
		{
			up_down_share.library.Utility.log(AppDelegate.class, "Throws exception (synchronizeSharedPreference).", ex);
		}
	}
}
